package cn.stylefeng.guns.modular.auth.controller;

import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import cn.stylefeng.guns.modular.auth.domain.dto.AuthPriorityDTO;
import cn.stylefeng.guns.modular.auth.domain.vo.AuthPriorityVO;
import cn.stylefeng.guns.modular.auth.entity.AuthPriorityEntity;
import cn.stylefeng.guns.modular.auth.service.AuthPriorityService;
import cn.stylefeng.guns.modular.auth.utils.converter.AuthPriorityDTO2VOConverter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author allen
 * @version 1.0
 * @date 2021/4/12 17:52
 */
@RestController
@RequestMapping("/auth/priority")
public class AuthPriorityController {


    @Resource
    private AuthPriorityDTO2VOConverter authPriorityDTO2VOConverter;

    @Resource
    private AuthPriorityService authPriorityService;

    /**
     * 查询跟权限
     *
     * @return 跟权限
     */
    @GetMapping("/root")
    public ResponseData listRootPriorities() {
        List<AuthPriorityDTO> authPriorityDTOS = authPriorityService.listRootPriorities();

        List<AuthPriorityVO> list = authPriorityDTO2VOConverter.sourceToTarget(authPriorityDTOS);
        return SuccessResponseData.success(list);

    }

    /**
     * 根据父权限id查询权限列表
     *
     * @param parentId id
     * @return 权限列表
     */
    @GetMapping("/listByParentId")
    public ResponseData getPrioritiesByParentId(@RequestParam(required = true) Long parentId) {
        List<AuthPriorityDTO> authPriorityDTOS = authPriorityService.getPrioritiesByParentId(parentId);

        return SuccessResponseData.success(authPriorityDTO2VOConverter.sourceToTarget(authPriorityDTOS));

    }


    /**
     * 插入或者更新权限
     *
     * @param record 实体
     * @return 处理结果
     */
    @PostMapping("/insertOrUpdate")
    public ResponseData insertOrUpdate(AuthPriorityEntity record) {
        authPriorityService.insertOrUpdate(record);

        return SuccessResponseData.success();
    }


    /**
     * 根据id查询权限vo
     *
     * @param id id
     * @return vo
     */
    @GetMapping("/{id}")
    public ResponseData getPrioritiesById(@PathVariable(required = true) Long id) {
        AuthPriorityDTO prioritiesById = authPriorityService.getPrioritiesById(id);
        AuthPriorityVO authPriorityVO = authPriorityDTO2VOConverter.sourceToTarget(prioritiesById);
        return SuccessResponseData.success(authPriorityVO);
//        return authPriorityConverter.dto2Vo(prioritiesById);


    }

    /**
     * 根据id删除权限
     *
     * @param id id
     * @return vo
     */
    @DeleteMapping("/priority/{id}")
    public ResponseData deletePriorityById(@PathVariable(required = true) Long id) {
        Boolean result = authPriorityService.deleteById(id);
        if (!result) {

            return ResponseData.error("处理失败");
        }
        return SuccessResponseData.success();


    }




}
